let express = require('express');
let mysql = require('./db')

let router = express.Router();

module.exports = router;

// 获取购物车数据列表
router.get('/list/:username', (req, res) => {
    let { username } = req.params;
    mysql.query(`select * from cart inner join menu on menu.dishes_id = cart.dishes_id where cart.username='${username}';`, (state, data) => {
        if (state) {
            mysql.query(`select count(*) as total from cart where username='${username}';`,(state1,total)=>{
                if (state1) { 
                        res.send({
                            code: 200,
                            msg: '请求成功',
                            total: total[0].total,
                            data
                        })
                } else {
                    res.send({
                        code: 400,
                        msg: '请求错误',
                        data
                    })
                }
            })
        } else {
            res.send({
                code: 400,
                msg: '查询数据失败',
                data,
            })
        }
    })
})

// 根据id获取购物车数据列表
router.get('/list/:dishes_id', (req, res) => {
    let { dishes_id,username } = req.params;
    mysql.query(`select * from cart  where dishes_id='${dishes_id}' and username = '${username}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '请求成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '查询数据失败',
                data,
            })
        }
    })
})

// 删除购物车中数据
// 所有用户 的购物车中数据都在一张表中
// 同一个商品 可能会有多个用户添加到购物车
// 如果只根据goods_id删除数据 会导致其他用户的该商品也会被删除
// 需要根据 username 和 goods_id 来删除数据
router.post('/deleteData', (req, res) => {
    let { username, dishes_id } = req.body;
    mysql.query(`delete from cart where username='${username}' and dishes_id ='${dishes_id}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '删除数据成功'
            })
        } else {
            res.send({
                code: 400,
                msg: '删除数据失败'
            })
        }
    })
})

// 清空购物车中数据
router.post('/clear', (req, res) => {
    let { username} = req.body;
    mysql.query(`delete from cart where username='${username}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '删除数据成功'
            })
        } else {
            res.send({
                code: 400,
                msg: '删除数据失败'
            })
        }
    })
})

// 修改购物车中数据
router.post('/updateData', (req, res) => {
    let { username, dishes_id, qty } = req.body;
    mysql.query(`update cart set qty=${qty} where username='${username}' and dishes_id=${dishes_id};`, (state) => {
        if (state) {
            res.send({
                code: 200,
                msg: '修改数据成功',
            })
        } else {
            res.send({
                code: 400,
                msg: '修改数据失败'
            })
        }
    })
})

// 往购物车中添加数据
router.post('/addData', (req, res) => {
    // goods_id username
    let { dishes_id, username } = req.body;
    // 添加之前得先判断这个用户的这条商品是否是存在的
    // 如果存在 就让这条商品的qty+1
    // 如果不存在就 增加这条商品
    mysql.query(`select * from cart where username='${username}' and dishes_id=${dishes_id};`, (state, data) => {
        if (state && data.length > 0) {
            // 说明数据已经存在
            let qty = (data[0].qty)*1 + 1;
            // console.log(qty);
            mysql.query(`update cart set qty=${qty} where username='${username}' and dishes_id=${dishes_id};`, (state1) => {
                if (state1) {
                    res.send({
                        code: 200,
                        msg: '添加商品成功'
                    })
                } else {
                    res.send({
                        code: 400,
                        msg: '添加商品失败'
                    })
                }

            })
        } else if (state && data.length == 0) {
            mysql.query(`insert into cart values(null,'${username}',${dishes_id},1)`, (state2) => {
                if (state2) {
                    res.send({
                        code: 200,
                        msg: '添加商品成功'
                    })
                } else {
                    res.send({
                        code: 400,
                        msg: '添加商品失败'
                    })
                }
            })
        }
    })
})

module.exports = router